package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DownloadHistory;
import com.ruoyi.system.service.IDownloadHistoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 下载记录Controller
 * 
 * @author ruoyi
 * @date 2023-12-12
 */
@Controller
@RequestMapping("/system/history")
public class DownloadHistoryController extends BaseController
{
    private String prefix = "system/history";

    @Autowired
    private IDownloadHistoryService downloadHistoryService;

    @RequiresPermissions("system:history:view")
    @GetMapping()
    public String history()
    {
        return prefix + "/history";
    }

    /**
     * 查询下载记录列表
     */
    @RequiresPermissions("system:history:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DownloadHistory downloadHistory)
    {
        startPage();
        List<DownloadHistory> list = downloadHistoryService.selectDownloadHistoryList(downloadHistory);
        return getDataTable(list);
    }

    /**
     * 导出下载记录列表
     */
    @RequiresPermissions("system:history:export")
    @Log(title = "下载记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DownloadHistory downloadHistory)
    {
        List<DownloadHistory> list = downloadHistoryService.selectDownloadHistoryList(downloadHistory);
        ExcelUtil<DownloadHistory> util = new ExcelUtil<DownloadHistory>(DownloadHistory.class);
        return util.exportExcel(list, "下载记录数据");
    }

    /**
     * 新增下载记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存下载记录
     */
    @RequiresPermissions("system:history:add")
    @Log(title = "下载记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DownloadHistory downloadHistory)
    {
        return toAjax(downloadHistoryService.insertDownloadHistory(downloadHistory));
    }

    /**
     * 修改下载记录
     */
    @RequiresPermissions("system:history:edit")
    @GetMapping("/edit/{historyId}")
    public String edit(@PathVariable("historyId") Long historyId, ModelMap mmap)
    {
        DownloadHistory downloadHistory = downloadHistoryService.selectDownloadHistoryByHistoryId(historyId);
        mmap.put("downloadHistory", downloadHistory);
        return prefix + "/edit";
    }

    /**
     * 修改保存下载记录
     */
    @RequiresPermissions("system:history:edit")
    @Log(title = "下载记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DownloadHistory downloadHistory)
    {
        return toAjax(downloadHistoryService.updateDownloadHistory(downloadHistory));
    }

    /**
     * 删除下载记录
     */
    @RequiresPermissions("system:history:remove")
    @Log(title = "下载记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(downloadHistoryService.deleteDownloadHistoryByHistoryIds(ids));
    }
}
